package org.icy.core.setup;

import java.util.ArrayList;

import org.ice.Config;
import org.ice.config.ConfigData;
import org.ice.config.ConfigSetup;
import org.ice.db.AdapterFactory;
import org.ice.module.router.IRouter;
import org.ice.registry.IRegistry;
import org.ice.registry.RegistryFactory;
import org.icy.modules.router.ExternalModuleRouter;

public class BaseConfigSetup implements ConfigSetup {

	@Override
	public void setup(ConfigData data) throws Exception {
		IRegistry reg = RegistryFactory.getRegistry();
		String installed = data.get("icy.app.status");
		if (installed == null || !installed.equals("run")) {
			reg.set("icy.app.install", false);
		} else {
			reg.set("icy.app.install", true);
		}
		reg.set("icy.app.status", installed);
		
		String theme = data.get("icy.app.theme");
		if (theme != null)
			reg.set("icy.app.theme", theme);
		else
			reg.set("icy.app.theme", "iceberg");
		
		if (installed.equals("db")) {
			String host = data.get("ice.db.host");
			String port = data.get("ice.db.port");
			String name = data.get("ice.db.name");
			String username = data.get("ice.db.username");
			String passwd = data.get("ice.db.password");
			String adapter = data.get("ice.db.adapter");
			AdapterFactory.setupAdapter(adapter, host, port, username, passwd, name);
		}
		
		ArrayList<IRouter> routers = new ArrayList<IRouter>();
		routers.add(new ExternalModuleRouter());
		
		Config.set("routers", routers);
		Config.set("icy.path.modules", data.get("icy.path.modules"));
		
		//Initialize module class loader
		
	}

}
